package com.atguigu.gmall.activity.config;

import com.atguigu.gmall.activity.component.SeckillLocalCache;
import com.atguigu.gmall.common.cache.GmallCacheAspect;
import com.atguigu.gmall.common.config.ItemServiceRedissonConfig;
import com.atguigu.gmall.common.config.MybatisPlusConfig;
import com.atguigu.gmall.common.constant.RedisConst;
import com.atguigu.gmall.feign.AppFeignConfig;
import com.atguigu.gmall.mq.AppRabbitConfig;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.context.annotation.Import;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
import org.springframework.scheduling.annotation.EnableScheduling;

import java.util.concurrent.*;

@EnableScheduling
@EnableAspectJAutoProxy
@EnableFeignClients({"com.atguigu.gmall.feign.order"})
@Configuration
@Import({
        MybatisPlusConfig.class,
        AppFeignConfig.class,
        ItemServiceRedissonConfig.class,
        GmallCacheAspect.class,
        AppRabbitConfig.class
})
public class ActivityConfig {


    @Bean
    RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory,SeckillLocalCache seckillLocalCache){
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        //订阅主题 seckillLocalCache这个监听器监听redis中，channel_key通道的消息
        container.addMessageListener(seckillLocalCache,new PatternTopic(SeckillLocalCache.channel_key));
        container.addMessageListener(seckillLocalCache,new PatternTopic(RedisConst.SECKILL_GOODS_OUT));
        //这个container可以添加多个messageListener
        return container;
    }

    /**
     * corePoolSize
     * keepAliveTime
     * maximumPoolSize
     * maximumPoolSize
     * workQueue
     * threadFactory 用自己的线程工厂，有uuid
     * RejectedExecutionHandler   DiscardOldestPolicy 抛弃老得消息
     * @return
     */
    @Bean
    ThreadPoolExecutor asyncExecutor(){

        return new ThreadPoolExecutor(
                2,
                2,
                1,
                TimeUnit.MINUTES,
                new LinkedBlockingQueue<>(4),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.DiscardOldestPolicy()
                );
    }
}
